﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using SubSonic;


public partial class User_Shares_ReportByYearAndCity : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LabelMessage.Text = "";
        LabelMessage.ForeColor = System.Drawing.Color.Red;
        LabelMessage.Font.Bold = true;
        if (!Page.IsPostBack)
        {
            Session["ContractIds"] = null;
            DropDownCity.DataSource = TblCity.FetchAll(OrderBy.Asc("Name"));
            DropDownCity.DataValueField = TblCity.Columns.Id;
            DropDownCity.DataTextField = TblCity.Columns.Name;
            DropDownCity.DataBind();
            SubSonic.Query q = new Query(TblContract.Schema);
            q.SetSelectList(TblContract.Columns.Year);
            q.DISTINCT();
            DropDownListYear.DataSource = q.ExecuteReader();
            DropDownListYear.DataValueField = TblContract.Columns.Year;
            DropDownListYear.DataTextField = TblContract.Columns.Year;
            DropDownListYear.DataBind();
        }
    }
    protected void DropDownCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["ContractIds"] = null;
    }
    protected void ButtonExcelOutput_Click(object sender, EventArgs e)
    {
        Guid cityId = Guid.NewGuid();
        RadioButton radioButtonAdvisor =  LoginViewSearch.FindControl( "RadioButtonAdvisor" ) as RadioButton;
        RadioButton radioButtonSurveyor = LoginViewSearch.FindControl("RadioButtonSurveyor") as RadioButton;
        if ( DropDownCity.SelectedValue != null )
            cityId= new Guid(DropDownCity.SelectedValue);
        int year=0;
        if ( DropDownListYear.SelectedValue.Trim() != "") 
            year= Convert.ToInt32( DropDownListYear.SelectedValue);
        List<Guid> output = new List<Guid>();
        List<DataAccess.TblContract> contracts = new List<TblContract>();
        if (CheckBoxCity.Checked && !CheckBoxYear.Checked )
        {
            contracts = Business.CommonCalculations.SelectProjectsByCity(cityId);
        }
        if (!CheckBoxCity.Checked && CheckBoxYear.Checked)
        {
            contracts = Business.CommonCalculations.SelectProjectsByYear(year);
        }
        if (CheckBoxCity.Checked && CheckBoxYear.Checked)
        {
            contracts = Business.CommonCalculations.SelectProjectsByCityAndYear(cityId, year);
        }
        if (!CheckBoxCity.Checked && !CheckBoxYear.Checked)
        {
            LabelMessage.Text = "هیچ پروژه ای برای خروجی انتخاب نشده";
            return; 
        }
        foreach (var contract in contracts)
        {
            //to filter Surveyor and Advisors I choosed this celever method.
            if (Convert.ToBoolean(contract.ContractType) && radioButtonAdvisor.Checked)
            {
                output.Add( contract.Id );
            }
            if(!Convert.ToBoolean(contract.ContractType) && radioButtonSurveyor.Checked)
            {
                output.Add(contract.Id);
            }
        }
        Session["ContractIds"] = output;

        Response.Redirect("~/User/Shares/ExcelOutputMultiple.aspx");
    }
    protected void CheckBoxCity_CheckedChanged(object sender, EventArgs e)
    {
        PanelCity.Enabled = CheckBoxCity.Checked;
    }
    protected void CheckBoxYear_CheckedChanged(object sender, EventArgs e)
    {
        PanelYear.Enabled = CheckBoxYear.Checked;
    }
}